package com.gxa.community.admin.controller.order;


import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.gxa.community.admin.service.order.OrderService;
import com.gxa.community.core.common.RegExpression;
import com.gxa.community.core.pojo.bo.order.OrderAndGoodsInfo;
import com.gxa.community.core.pojo.vo.order.GoodsInfoVo;
import com.gxa.community.core.pojo.vo.order.OrderInfoVo;
import com.gxa.community.core.utils.LittleUtils;
import com.gxa.community.core.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.regex.Pattern;

/**
 * 订单信息表 前端控制器
 *
 * @author silver
 * @since 2022-07-19
 */
@Slf4j
@RestController
@RequestMapping("/order")
public class OrderController {

    final OrderService orderService;

    public OrderController(OrderService orderService) {
        this.orderService = orderService;
    }

    /**
     * 创建订单
     *
     * @return 成功与否
     */
    @PostMapping("/createOrder")
    public R createOrder(@RequestBody OrderAndGoodsInfo info) {
        List<GoodsInfoVo> gs = info.getGoodsInfo();
        OrderInfoVo o = info.getOrderInfo();

        if (gs == null || gs.isEmpty()) return R.error("订单物品不能为空");
        if (o == null) return R.error("订单信息不能为空");

        // 校验商品信息
        for (GoodsInfoVo g : gs) {
            if (StringUtils.isBlank(g.getNos()))
                return R.error("商品编号有误");
            if (g.getGoodsNum() == null || g.getGoodsNum() < 1)
                return R.error("商品数量有误");
        }

        // 校验订单信息
        if (o.getAgentId() == null) return R.error("团长编号不能为空");
        if (o.getBuyerId() == null) return R.error("下单人编号不能为空");
        if (StringUtils.isBlank(o.getReceiverName())) return R.error("收获人姓名不能为空");
        if (StringUtils.isNotBlank(o.getReceiverPhone()))
            if (!Pattern.matches(RegExpression.PHONE_REG.getExpression(), o.getReceiverPhone()))
                return R.error("收货人手机号有误");
        if (LittleUtils.checkNum(o.getType(), 1, 3)) return R.error("订单类型有误");

        return orderService.createOrder(info);
    }

    /**
     * 查询订单
     *
     * @return 订单列表
     */
    @PostMapping("/queryOrder")
    public R queryOrder(Integer page, Integer size, @RequestBody OrderInfoVo o) {
        // 数据校验
        if (page == null || page < 1) page = 1;
        if (size == null || size < 1 || size > 100) size = 5;

        if (LittleUtils.checkNum(o.getType(), 1, 3)) o.setType(null);

        int i = LittleUtils.checkStartEndTime(o.getStartTime(), o.getEndTime());
        if (i == 1)
            return R.error("起始时间不能大于结束时间");
        else if (i == 2)
            return R.error("时间范围不允许超过两个月");

        if (StringUtils.isNotBlank(o.getReceiverPhone()))
            if (!(o.getReceiverPhone().length() > 0 && o.getReceiverPhone().length() <5))
                return R.error("手机号有误");

        if (LittleUtils.checkNum(o.getStatus(), 0, 3)) o.setStatus(null);

        if (StringUtils.isNotBlank(o.getReceiverPhone()) &&  o.getReceiverPhone().length() == 5)
            if (!Pattern.matches(RegExpression.PHONE_REG.getExpression(), o.getReceiverPhone()))
                return R.error("收货人手机号有误");

        return orderService.queryOrder(page, size, o);
    }

    /**
     * 根据订单编号查询订单内商品
     */
    @PostMapping("/queryGoods")
    public R queryGoods(@RequestBody List<String> orderNo) {
        return orderService.queryGoods(orderNo);
    }

}
